package com.yyw.gulimall.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yyw.common.utils.PageUtils;
import com.yyw.common.utils.Query;
import com.yyw.gulimall.ware.dao.PurchaseDetailDao;
import com.yyw.gulimall.ware.entity.PurchaseDetailEntity;
import com.yyw.gulimall.ware.service.PurchaseDetailService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.Objects;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<PurchaseDetailEntity> queryWrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        String status = (String) params.get("status");
        String wareId = (String) params.get("wareId");

        if (!StringUtils.isEmpty(key)) {
            queryWrapper.lambda().and(e -> {
                e.eq(PurchaseDetailEntity::getPurchaseId, key).or().eq(PurchaseDetailEntity::getSkuId, key);
            });
        }

        if (!StringUtils.isEmpty(status) && !Objects.equals("0", status)) {
            queryWrapper.lambda().eq(PurchaseDetailEntity::getStatus, status);
        }

        if (!StringUtils.isEmpty(wareId) && !Objects.equals("0", wareId)) {
            queryWrapper.lambda().eq(PurchaseDetailEntity::getWareId, wareId);
        }

        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

}